<?php

/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category Zeed
 * @package Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author Zeed Team (http://blog.zeed.com.cn)
 * @since 2010-12-6
 * @version SVN: $Id$
 */
class StatShowController extends ArchiveAdminAbstract
{
    /**
     * 数据统计 - 数据展示
     */
    public function index()
    {
        $this->addResult(self::RS_SUCCESS, 'json');

        /* 获取承办部门 */
        $where_department = array('status' => 1);
        $order_department = "sort_order ASC";
        $departments = Archive_Model_Department::instance()->fetchByWhere($where_department, $order_department);

        $data['departments'] = $departments;
        $this->setData('data', $data);
        $this->addResult(self::RS_SUCCESS, 'php', 'statshow.index');
        return parent::multipleResult(self::RS_SUCCESS);
    }

    /**
     * ajax 获取数据集合
     */
    public function fetchStatisticsData()
    {
        $this->addResult(self::RS_SUCCESS, 'json');

        /* 接收参数 */
        $params = $this->input->post();

        try {
            /* 获取所有的region_id */
            $region_list = Trend_Model_Region::instance()->fetchRegionIdByHid($params['region']);
            $params['region_list'] = $region_list;

            /* 获取案件相关数据 */
            $case_count_obj = $this->fetchCaseInfo($params);

            /* 获取行动相关数据 */
            $card_count_obj = $this->fetchCardInfo($params);

            /* 以求交集的方式，计算出案件和行动的统计数的最终结果 - 今年 */
            $content_sns = array_intersect($case_count_obj['all_content_sns'], $card_count_obj['all_content_sns']);
            $card_ids = array_intersect($case_count_obj['all_card_ids'], $card_count_obj['all_card_ids']);
            $case_count_obj['all_count'] = count($content_sns);
            $card_count_obj['all_count'] = count($card_ids);

            /* 以求交集的方式，计算出案件和行动的统计数的最终结果 - 去年 */
            $content_sns_last_year = array_intersect($case_count_obj['all_content_sns_last_year'], $card_count_obj['all_content_sns_last_year']);
            $card_ids_last_year = array_intersect($case_count_obj['all_card_ids_last_year'], $card_count_obj['all_card_ids_last_year']);
            $case_count_obj['all_year_count'] = count($content_sns_last_year);
            $card_count_obj['all_year_count'] = count($card_ids_last_year);

            /* 求案件的同比 */
            $case_count_obj['all_rate'] = $case_count_obj['all_year_count'] > 0 ?
                ($case_count_obj['all_count'] - $case_count_obj['all_year_count']) / $case_count_obj['all_year_count'] : $case_count_obj['all_count'];

            /* 求行动的同比 */
            $card_count_obj['all_rate'] = $card_count_obj['all_year_count'] > 0 ?
                ($card_count_obj['all_count'] - $card_count_obj['all_year_count']) / $card_count_obj['all_year_count'] : $card_count_obj['all_count'];

            /* 获取图片、音视频等信息 */
            $attachment_count_obj = $this->fetchAttachmentInfo($card_ids, $card_ids_last_year);

            /* 获取每月、季度、年的交集信息 */
            $result_case_card = $this->intersectCaseAndCard($case_count_obj, $card_count_obj);
            $case_count_obj['case_count_list'] = $result_case_card['case_count_list'];
            $case_count_obj['lase_case_count_list'] = $result_case_card['case_count_list_last_year'];
            $case_count_obj['rate_list'] = $result_case_card['case_rate_obj'];
            $card_count_obj['case_count_list'] = $result_case_card['card_count_list'];
            $card_count_obj['lase_case_count_list'] = $result_case_card['card_count_list_last_year'];
            $card_count_obj['rate_list'] = $result_case_card['card_rate_obj'];

            /* 获取 X 轴坐标数据 */
            switch ($params['statistics_type']) {
                case '1':
                    $data['xLabel'] = $this->showMonthRange($params['start_time'], $params['end_time']);
                    break;
                case '2':
                    $data['xLabel'] = array('第一季度', '第二季度', '第三季度', '第四季度');
                    break;
                case '3':
                    $data['xLabel'] = $this->getYearXLabel($params['start_time'], $params['end_time']);
                    break;
                default:
                    break;
            }

            /* 去除冗余数据 */
            unset($case_count_obj['all_content_sns'], $case_count_obj['all_card_ids'], $case_count_obj['all_content_sns_last_year'],
                $case_count_obj['all_card_ids_last_year'], $case_count_obj['case_info_list'], $case_count_obj['card_info_list'],
                $case_count_obj['last_case_info_list'], $case_count_obj['last_card_info_list']);
            unset($card_count_obj['all_content_sns'], $card_count_obj['all_card_ids'], $card_count_obj['all_content_sns_last_year'],
                $card_count_obj['all_card_ids_last_year'], $card_count_obj['card_case_info_list'], $card_count_obj['card_card_info_list'],
                $card_count_obj['last_card_case_info_list'], $card_count_obj['last_card_card_info_list']);

            $data['case_count_obj'] = $case_count_obj;
            $data['case_data_count_obj'] = $card_count_obj;
            $data['video_count_obj'] = array_merge($attachment_count_obj['video_count_obj'], $result_case_card['video_count_list']);
            $data['audio_count_obj'] = array_merge($attachment_count_obj['audio_count_obj'], $result_case_card['audio_count_list']);
            $data['photo_count_obj'] = array_merge($attachment_count_obj['photo_count_obj'], $result_case_card['photo_count_list']);

            $this->setData('data', $data);
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError($e->getMessage());
        }
        return self::RS_SUCCESS;
    }

    /**
     * 获取案件的集合
     */
    private function fetchCaseInfo($params)
    {
        /* 处理时间参数 */
        $caseTimeList = [];
        if ($params['start_time'] && $params['end_time']) {
            $caseTimeList = $this->handleTimeInfo($params, $params['start_time'], $params['end_time']);
        }

        /* 获取参数时间范围内的案件数据 */
        $case_count_list = $case_info_list = $card_info_list = [];
        $last_case_count_list = $last_case_info_list = $last_card_info_list = [];
        if (count($caseTimeList) > 0) {
            foreach ($caseTimeList as $k => $item) {
                $result = $this->fetchCaseCountByTime($params, $item['start_time'], $item['end_time']);
                $case_count_list[] = $result['count'];
                $last_case_count_list[] = $result['year_count'];
                $case_info_list[$k] = $result['content_sns'];
                $card_info_list[$k] = $result['card_ids'];
                $last_case_info_list[$k] = $result['content_sns_last_year'];
                $last_card_info_list[$k] = $result['card_ids_last_year'];
            }
        }

        /* 获取总的案件数据 */
        if ($params['statistics_type'] == 2) {
            $start_time = date('Y', strtotime($params['start_time'])) . '-01-01';
            $end_time = date('Y', strtotime($params['start_time'])) . '-12-31';
        } else {
            $start_time = $params['start_time'];
            $end_time = $params['end_time'];
        }
        $all_result = $this->fetchCaseCountByTime($params, $start_time, $end_time);

        /* 汇总数据并返回 */
        return array(
            'all_count' => $all_result['count'], // 今年案件总数
            'all_year_count' => $all_result['count_last_year'], // 去年案件总数
            'all_content_sns' => $all_result['content_sns'], // 今年案件编号集合
            'all_card_ids' => $all_result['card_ids'], // 今年案件所属行动的 ID 集合
            'all_content_sns_last_year' => $all_result['content_sns_last_year'], // 去年案件编号集合
            'all_card_ids_last_year' => $all_result['card_ids_last_year'], // 去年案件所属行动的 ID 集合
            'case_info_list' => $case_info_list, // 今年指定时间范围内的案件列表信息
            'card_info_list' => $card_info_list, // 今年指定时间范围内的案件所属行动列表信息
            'last_case_info_list' => $last_case_info_list, // 前一年指定时间范围内的案件列表信息
            'last_card_info_list' => $last_card_info_list, // 前一年指定时间范围内的行动列表信息
        );
    }

    /**
     * 获取行动的集合
     */
    private function fetchCardInfo($params)
    {
        /* 处理时间参数 */
        $caseTimeList = [];
        if ($params['start_time'] && $params['end_time']) {
            $caseTimeList = $this->handleTimeInfo($params, $params['start_time'], $params['end_time']);
        }

        /* 获取参数时间范围内的行动数据 */
        $card_count_list = $card_case_info_list = $card_card_info_list = [];
        $last_card_count_list = $last_card_case_info_list = $last_card_card_info_list = [];
        if (count($caseTimeList) > 0) {
            foreach ($caseTimeList as $k => $item) {
                $result = $this->fetchCardCountByTime($params, $item['start_time'], $item['end_time']);
                $card_count_list[] = $result['count'];
                $last_card_count_list[] = $result['count_last_year'];
                $card_case_info_list[$k] = $result['content_sns'];
                $card_card_info_list[$k] = $result['card_ids'];
                $last_card_case_info_list[$k] = $result['content_sns_last_year'];
                $last_card_card_info_list[$k] = $result['card_ids_last_year'];
            }
        }

        /* 获取总的行动数据 */
        if ($params['statistics_type'] == 2) {
            $start_time = date('Y', strtotime($params['start_time'])) . '-01-01';
            $end_time = date('Y', strtotime($params['start_time'])) . '-12-31';
        } else {
            $start_time = $params['start_time'];
            $end_time = $params['end_time'];
        }
        $all_result = $this->fetchCardCountByTime($params, $start_time, $end_time);

        /* 汇总数据并返回 */
        return array(
            'all_count' => $all_result['count'], // 今年行动总数
            'all_year_count' => $all_result['count_last_year'], // 去年行动总数
            'all_content_sns' => $all_result['content_sns'], // 今年所属案件编号集合
            'all_card_ids' => $all_result['card_ids'], // 今年行动的 ID 集合
            'all_content_sns_last_year' => $all_result['content_sns_last_year'], // 去年所属案件编号集合
            'all_card_ids_last_year' => $all_result['card_ids_last_year'], // 去年行动的 ID 集合
            'card_case_info_list' => $card_case_info_list, // 今年指定时间范围内的行动所对应案件列表信息
            'card_card_info_list' => $card_card_info_list, // 今年指定时间范围内的行动列表信息
            'last_card_case_info_list' => $last_card_case_info_list, // 前一年指定时间范围内的行动所对应案件列表信息
            'last_card_card_info_list' => $last_card_card_info_list, // 前一年指定时间范围内的行动列表信息
        );
    }

    /**
     * 获取指定行动下资源（图片、音视频）信息的集合
     */
    private function fetchAttachmentInfo($card_ids, $card_ids_last_year)
    {
        /* 获取指定行动信息 */
        $cards = Archive_Model_Card::instance()->fetchByPK(array_values($card_ids));
        $cards_last_year = Archive_Model_Card::instance()->fetchByPK(array_values($card_ids_last_year));

        /* 获取图片、音视频信息 */
        $attachment_count_obj = $this->fetchAttachmentDetailCount($cards);
        $attachment_count_obj_last_year = $this->fetchAttachmentDetailCount($cards_last_year);

        /* 计算同比 */
        $rate_video = $attachment_count_obj_last_year['video_duration'] > 0 ?
            ($attachment_count_obj['video_duration'] - $attachment_count_obj_last_year['video_duration']) /
            $attachment_count_obj_last_year['video_duration'] : $attachment_count_obj['video_duration'];
        $rate_audio = $attachment_count_obj_last_year['audio_duration'] > 0 ?
            ($attachment_count_obj['audio_duration'] - $attachment_count_obj_last_year['audio_duration']) /
            $attachment_count_obj_last_year['audio_duration'] : $attachment_count_obj['audio_duration'];
        $rate_photo = $attachment_count_obj_last_year['photo_count'] > 0 ?
            ($attachment_count_obj['photo_count'] - $attachment_count_obj_last_year['photo_count']) /
            $attachment_count_obj_last_year['photo_count'] : $attachment_count_obj['photo_count'];

        return array(
            'video_count_obj' => array(
                'all_count' => Util_Timer::timeFormatMinute($attachment_count_obj['video_duration']),
                'all_year_count' => Util_Timer::timeFormatMinute($attachment_count_obj_last_year['video_duration']),
                'all_rate' => number_format($rate_video, 2, '.', ''),
            ),
            'audio_count_obj' => array(
                'all_count' => Util_Timer::timeFormatMinute($attachment_count_obj['audio_duration']),
                'all_year_count' => Util_Timer::timeFormatMinute($attachment_count_obj_last_year['audio_duration']),
                'all_rate' => number_format($rate_audio, 2, '.', ''),
            ),
            'photo_count_obj' => array(
                'all_count' => $attachment_count_obj['photo_count'],
                'all_year_count' => $attachment_count_obj_last_year['photo_count'],
                'all_rate' => number_format($rate_photo, 2, '.', ''),
            ),
        );
    }

    /**
     * 获取指定时间段内的案件信息及同比去年时间段内的案件信息
     */
    private function fetchCaseCountByTime($params, $start_time = null, $end_time = null)
    {
        /* 组织查询条件 */
        $where = $where_last_year = array('status' => array('$in' => array(-2, 1)));

        /* 处理时间 */
        if ($start_time && $end_time) {
            $where['$and'][] = $this->handleTimeWhere($params, $start_time, $end_time, 'tte', 'Y-m-d');

            // 同比上一年的时间处理的
            $last_start_time = date('Y-m-d', strtotime("-1 years", strtotime($start_time)));
            $last_end_time = date('Y-m-d', strtotime("-1 years", strtotime($end_time)));
            $where_last_year['$and'][] = $this->handleTimeWhere($params, $last_start_time, $last_end_time, 'tte', 'Y-m-d');
        }
        if ($params['department_name']) {
            $department_name = $params['department_name'];
            $where['$and'][] = array('department_name' => new MongoRegex("/$department_name/i"));
            $where_last_year['$and'][] = array('department_name' => new MongoRegex("/$department_name/i"));
        }

        if ($params['key']) {
            $key = $params['key'];
            $where['$and'][] = array(
                '$or' => array(
                    array('title' => new MongoRegex("/$key/i")),
                    array('respondent' => new MongoRegex("/$key/i"))
                )
            );
            $where_last_year['$and'][] = array(
                '$or' => array(
                    array('title' => new MongoRegex("/$key/i")),
                    array('respondent' => new MongoRegex("/$key/i"))
                )
            );
        }

        /* 获取数据量的统计信息 */
        $count = Archive_Model_Content::instance()->getCount($where);
        $count_last_year = Archive_Model_Content::instance()->getCount($where_last_year);

        /* 获取数据统计信息 - 今年 */
        $content_sns = array();
        $contents = Archive_Model_Content::instance()->fetchByWhere($where, null, null, null, array('sn'));
        if (! empty($contents)) {
            foreach ($contents as $k => $v) {
                $content_sns[] = $v['sn'];
            }
        }

        /* 获取案件下属的行动统计信息 - 今年 */
        $card_ids = array();
        if (! empty($content_sns)) {
            $where_card = array(
                'status' => array('$in' => array(-2, 1)),
                'sn' => array('$in' => $content_sns),
            );
            $cards = Archive_Model_Card::instance()->fetchByWhere($where_card, null, null, null, array('_id'));
            if (! empty($cards)) {
                foreach ($cards as $k => $v) {
                    $card_ids[] = $v['_id'];
                }
            }
        }

        /* 获取数据统计信息 - 去年 */
        $content_sns_last_year = array();
        $contents_last_year = Archive_Model_Content::instance()->fetchByWhere($where_last_year, null, null, null, array('sn'));
        if (! empty($contents_last_year)) {
            foreach ($contents_last_year as $k => $v) {
                $content_sns_last_year[] = $v['sn'];
            }
        }

        /* 获取案件下属的行动统计信息 - 去年 */
        $card_ids_last_year = array();
        if (! empty($content_sns_last_year)) {
            $where_card_last_year = array(
                'status' => array('$in' => array(-2, 1)),
                'sn' => array('$in' => $content_sns_last_year),
            );
            $cards_last_year = Archive_Model_Card::instance()->fetchByWhere($where_card_last_year, null, null, null, array('_id'));
            if (! empty($cards_last_year)) {
                foreach ($cards_last_year as $k => $v) {
                    $card_ids_last_year[] = $v['_id'];
                }
            }
        }

        /* 返回 */
        return array(
            'count' => $count,
            'content_sns' => $content_sns,
            'card_ids' => $card_ids,
            'count_last_year' => $count_last_year,
            'content_sns_last_year' => $content_sns_last_year,
            'card_ids_last_year' => $card_ids_last_year,
        );
    }

    /**
     * 获取指定时间段内的行动信息
     */
    private function fetchCardCountByTime($params, $start_time = null, $end_time = null)
    {
        /* 组织查询条件 */
        $where = $where_last_year = array('status' => array('$in' => array(-2, 1)));

        /* 处理地区 */
        if ($params['region_list']) {
            $where['region_id'] = $where_last_year['region_id'] = array('$in' => $params['region_list']);
        }

        /* 处理时间排序 */
        if ($start_time && $end_time) {
            $where['$and'][] = $this->handleTimeWhere($params, $start_time, $end_time, 'tte_start', 'Y-m-d H:i:s');

            // 同比上一年的时间处理的
            $last_start_time = date('Y-m-d', strtotime("-1 years", strtotime($start_time)));
            $last_end_time = date('Y-m-d', strtotime("-1 years", strtotime($end_time)));
            $where_last_year['$and'][] = $this->handleTimeWhere($params, $last_start_time, $last_end_time, 'tte_start', 'Y-m-d H:i:s');
        }

        if ($params['key']) {
            $key = $params['key'];
            $where['$and'][] = array(
                '$or' => array(
                    array('title' => new MongoRegex("/$key/i")),
                    array('respondent' => new MongoRegex("/$key/i"))
                )
            );
            $where_last_year['$and'][] = array(
                '$or' => array(
                    array('title' => new MongoRegex("/$key/i")),
                    array('respondent' => new MongoRegex("/$key/i"))
                )
            );
        }

        /* 获取数据量的统计信息 */
        $count = Archive_Model_Card::instance()->getCount($where);
        $count_last_year = Archive_Model_Card::instance()->getCount($where_last_year);

        /* 获取数据统计信息 - 今年 */
        $card_ids = $content_sns = array();
        $cards = Archive_Model_Card::instance()->fetchByWhere($where, null, null, null, array('sn'));
        if (! empty($cards)) {
            foreach ($cards as $k => $v) {
                $card_ids[] = $v['_id'];
                $content_sns[] = $v['sn'];
            }
        }

        /* 获取数据统计信息 - 去年 */
        $card_ids_last_year = $content_sns_last_year = array();
        $cards_last_year = Archive_Model_Card::instance()->fetchByWhere($where_last_year, null, null, null, array('sn'));
        if (! empty($cards_last_year)) {
            foreach ($cards_last_year as $k => $v) {
                $card_ids_last_year[] = $v['_id'];
                $content_sns_last_year[] = $v['sn'];
            }
        }

        /* 返回 */
        return array(
            'count' => $count,
            'content_sns' => array_unique($content_sns),
            'card_ids' => $card_ids,
            'count_last_year' => $count_last_year,
            'content_sns_last_year' => array_unique($content_sns_last_year),
            'card_ids_last_year' => $card_ids_last_year,
        );
    }

    /**
     * 获取指定行动下的图片张数、音视频长度信息
     */
    private function fetchAttachmentDetailCount($cards)
    {
        $result = array(
            'video_duration' => 0,
            'audio_duration' => 0,
            'photo_count' => 0
        );

        if (! empty($cards)) {
            foreach ($cards as $item) {
                if ($item['duration_audio'] != 0) {
                    if (is_object($item['duration_audio'])) {
                        $result['audio_duration'] += (int) ($item['duration_audio']->value);
                    } else {
                        $result['audio_duration'] += $item['duration_audio'];
                    }
                }

                if ($item['duration_video'] != 0) {
                    if (is_object($item['duration_video'])) {
                        $result['video_duration'] += (int) ($item['duration_video']->value);
                    } else {
                        $result['video_duration'] += $item['duration_video'];
                    }
                }

                if ($item['attachments_img']) {
                    $result['photo_count'] += count(explode(',', $item['attachments_img']));
                }
            }
        }

        /* 时间转化秒为分钟 */
        $result['video_duration'] = ceil($result['video_duration'] / 60);
        $result['audio_duration'] = ceil($result['audio_duration'] / 60);

        return $result;
    }

    /**
     * 处理查询的日期
     */
    private function handleTimeWhere($params, $start_time, $end_time, $field, $time_format)
    {
        /* 时间的初步处理 */
        $time_se = array($start_time, $end_time);
        asort($time_se);
        $time_se = array_values($time_se);
        $time_start = $time_se[0];
        $time_end = $time_se[1];

        /* 组织查询条件 */
        $result = array();
        switch ($params['statistics_type']) {
            /* 按月 */
            case '1':
                $time_end_arr = explode('-', $time_end);
                if (count($time_end_arr) == 2) {
                    $time_end = $time_end . '-01';
                } else {
                    unset($time_end_arr[2]);
                    $time_end = implode('-', $time_end_arr);
                }
                $result = array(
                    '$and' => array(
                        array($field => ['$gte' => date($time_format, strtotime($time_start))]),
                        array($field => ['$lte' => date($time_format, strtotime(($time_end . '-01') . " + 1 month - 1 sec"))])
                    )
                );
                break;

            /* 按季度 */
            case '2':
                $result = array(
                    '$and' => array(
                        array($field => ['$gte' => date($time_format, strtotime($time_start))]),
                        array($field => ['$lte' => date($time_format, strtotime($time_end))])
                    )
                );
                break;

            /* 按年 */
            case '3':
                $result = array(
                    '$and' => array(
                        array($field => ['$gte' => date($time_format, strtotime($time_start))]),
                        array($field => ['$lte' => date($time_format, strtotime($time_end))])
                    )
                );
                break;

            default:
                break;
        }
        return $result;
    }

    /**
     * 获取月份list
     */
    private function showMonthRange($start, $end)
    {
        $end = date('Y-m', strtotime($end)); // 转换为月
        $range = [];
        $i = 0;
        do {
            $month = date('Y-m', strtotime($start . ' + ' . $i . ' month'));
            $range[] = $month;
            $i++;
        } while ($month < $end);

        return $range;
    }

    /**
     * 获取某个季度的开始和结束时间
     */
    private function getQuarterDate($date, $season)
    {
        $year = date('Y', strtotime($date));
        $times = array();
        $times['st'] = date('Y-m-d', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, $year));
        $times['et'] = date('Y-m-d', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, $year)), $year));
        return $times;
    }

    private function getYearXLabel($start_day, $end_day)
    {
        $start_year = date('Y', strtotime($start_day));
        $end_year = date('Y', strtotime($end_day));

        $result = [];
        for ($i = 0; $i <= abs($end_year - $start_year); $i++) {
            $result[] = ($start_year + $i) . '年';
        }
        return $result;
    }

    private function getYearDate($start_day, $end_day)
    {
        $start_year = date('Y', strtotime($start_day));
        $end_year = date('Y', strtotime($end_day));

        $result = [];
        for ($i = 0; $i <= abs($end_year - $start_year); $i++) {
            $result[] = array(
                'start_time' => ($start_year + $i) . '-01-01',
                'end_time' => ($start_year + $i) . '-12-31'
            );
        }
        return $result;
    }

    /**
     * 处理时间
     */
    private function handleTimeInfo($params, $start_day, $end_day)
    {
        $result = [];
        switch ($params['statistics_type']) {
            case '1':
                $dateList = $this->showMonthRange($start_day, $end_day);
                foreach ($dateList as $item) {
                    $days = date('t', strtotime($item));
                    $start_time = $item . '-01';
                    $end_time = $item . '-' . $days;
                    $result[] = array(
                        'start_time' => $start_time,
                        'end_time' => $end_time,
                    );
                }
                break;
            case '2':
                for ($i = 1; $i <= 4; $i++) {
                    $quarterDateResult = $this->getQuarterDate($start_day, $i);
                    $result[] = array(
                        'start_time' => $quarterDateResult['st'],
                        'end_time' => $quarterDateResult['et'],
                    );
                }
                break;
            case '3':
                $result = $this->getYearDate($start_day, $end_day);
                break;
            default:
                break;
        }
        return $result;
    }

    /**
     * 获取每月、季度、年的交集信息
     */
    private function intersectCaseAndCard($case_info, $card_info)
    {
        /* 处理今年的案件交集 */
        $case_count_list = array();
        if (isset($case_info['case_info_list']) && count($case_info['case_info_list']) > 0) {
            foreach ($case_info['case_info_list'] as $k => $v) {
                $case_count_list[$k] = count(array_intersect($v, $card_info['card_case_info_list'][$k]));
            }
        }

        /* 处理今年的行动交集 */
        $card_info_list = $card_count_list = array();
        if (isset($case_info['card_info_list']) && count($case_info['card_info_list']) > 0) {
            foreach ($case_info['card_info_list'] as $k => $v) {
                $card_info_list[$k] = array_intersect($v, $card_info['card_card_info_list'][$k]);
                $card_count_list[$k] = count($card_info_list[$k]);
            }
        }

        /* 处理去年的案件交集 */
        $case_count_list_last_year = array();
        if (isset($case_info['last_case_info_list']) && count($case_info['last_case_info_list']) > 0) {
            foreach ($case_info['last_case_info_list'] as $k => $v) {
                $case_count_list_last_year[$k] = count(array_intersect($v, $card_info['last_card_case_info_list'][$k]));
            }
        }

        /* 处理去年的行动交集 */
        $card_info_list_last_year = $card_count_list_last_year = array();
        if (isset($case_info['last_card_info_list']) && count($case_info['last_card_info_list']) > 0) {
            foreach ($case_info['last_card_info_list'] as $k => $v) {
                $card_info_list_last_year[$k] = array_intersect($v, $card_info['last_card_card_info_list'][$k]);
                $card_count_list_last_year[$k] = count($card_info_list_last_year[$k]);
            }
        }

        /* 求案件的同比 */
        $case_rate_obj = array();
        if (! empty($case_count_list)) {
            foreach ($case_count_list as $k => $v) {
                $case_rate_obj[$k] = $case_count_list_last_year[$k] > 0 ?
                    ($v - $case_count_list_last_year[$k]) / $case_count_list_last_year[$k] : $v;
            }
        }

        /* 求行动的同比 */
        $card_rate_obj = array();
        if (! empty($card_count_list)) {
            foreach ($card_count_list as $k => $v) {
                $card_rate_obj[$k] = $card_count_list_last_year[$k] > 0 ?
                    ($v - $card_count_list_last_year[$k]) / $card_count_list_last_year[$k] : $v;
            }
        }

        /* 求图片、音视频同比 */
        $video_count_list = $audio_count_list = $photo_count_list = array();
        foreach ($card_info_list as $k => $v) {
            $attachment_info = $this->fetchAttachmentInfo($v, $card_info_list_last_year[$k]);
            $video_count_list['case_count_list'][$k] = $attachment_info['video_count_obj']['all_count'];
            $video_count_list['lase_case_count_list'][$k] = $attachment_info['video_count_obj']['all_year_count'];
            $video_count_list['rate_list'][$k] = $attachment_info['video_count_obj']['all_rate'];

            $audio_count_list['case_count_list'][$k] = $attachment_info['audio_count_obj']['all_count'];
            $audio_count_list['lase_case_count_list'][$k] = $attachment_info['audio_count_obj']['all_year_count'];
            $audio_count_list['rate_list'][$k] = $attachment_info['audio_count_obj']['all_rate'];

            $photo_count_list['case_count_list'][$k] = $attachment_info['photo_count_obj']['all_count'];
            $photo_count_list['lase_case_count_list'][$k] = $attachment_info['photo_count_obj']['all_year_count'];
            $photo_count_list['rate_list'][$k] = $attachment_info['photo_count_obj']['all_rate'];
        }

        $result = array(
            'case_count_list' => $case_count_list,
            'card_count_list' => $card_count_list,
            'case_count_list_last_year' => $case_count_list_last_year,
            'card_count_list_last_year' => $card_count_list_last_year,
            'case_rate_obj' => $case_rate_obj,
            'card_rate_obj' => $card_rate_obj,
            'video_count_list' => $video_count_list,
            'audio_count_list' => $audio_count_list,
            'photo_count_list' => $photo_count_list,
        );
        return $result;
    }
}

// End ^ LF ^ encoding
